package com.classes;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.mbbmap.app.manager.ConfigManager;
import com.mbbmap.app.manager.DBConnectionManager;
import com.mbbmap.util.Constants;

public class InterchangeFactory {
	private String message;
	private long outletID = 0;
	
	public String getMessage() {
		return "[InterchangeFactory] " + this.message; 
	}
	
	public long getOutletID() {
		return this.outletID; 
	}
	
	
	public Interchange getMccRate(String if_id){
		try{
	        int TIMEOUT = 10000; //DB connection timeout if unable to get connection in less than 10 seconds
	        String connectionPool = ConfigManager.getInstance().getConnectionPool();
	        DBConnectionManager connMgr = DBConnectionManager.getInstance();
	        try { TIMEOUT = Integer.parseInt(ConfigManager.getInstance().get(Constants.DB_WAIT_TIME)); } catch (Exception e) {}
	        Connection conn = connMgr.getConnection(connectionPool, TIMEOUT);
			
			try {
				String sql = "select ifmcc.ifm_code, ifdetails.* "
						+ "from ifmcc "
						+ "join ifdetails on ifmcc.ifm_ifid=ifdetails.if_id "
						+ "where ifm_code = ?";
				
				PreparedStatement statement = conn.prepareStatement(sql);
				
				try {
					statement.setString(1, if_id);
					
					ResultSet resultSet = statement.executeQuery();
					
					try {
						if(resultSet.next()){
							Interchange i = new Interchange();
							
							i.setIfvln(resultSet.getString("if_vln"));
							i.setIfvlh(resultSet.getString("if_vlh"));
							i.setIfvlm(resultSet.getString("if_vlm"));
							i.setIfvlec(resultSet.getString("if_vlec"));
							i.setIfvfn(resultSet.getString("if_vfn"));
							i.setIfvfh(resultSet.getString("if_vfh"));
							i.setIfvfm(resultSet.getString("if_vfm"));
							i.setIfvfec(resultSet.getString("if_vfec"));
							i.setIfmcln(resultSet.getString("if_mcln"));
							i.setIfmclh(resultSet.getString("if_mclh"));
							i.setIfmclm(resultSet.getString("if_mclm"));
							i.setIfmclec(resultSet.getString("if_mclec"));
							i.setIfmcfn(resultSet.getString("if_mcfn"));
							i.setIfmcfh(resultSet.getString("if_mcfh"));
							i.setIfmcfm(resultSet.getString("if_mcfm"));
							i.setIfmcfec(resultSet.getString("if_mcfec"));
							
							return i;
						}
					} finally {
						resultSet.close();
					}
				} finally {
					statement.close();
				}
			
			} finally {
				connMgr.freeConnection(connectionPool, conn);
			}
			
			return null;
		}
		catch (Exception e){
			this.message ="Mcc interchange rate not found";
			return null;
		}
	
	}
	
	public Interchange getMcc(int ifm_mcc) {
		try {
	        int TIMEOUT = 10000; //DB connection timeout if unable to get connection in less than 10 seconds
	        String connectionPool = ConfigManager.getInstance().getConnectionPool();
	        DBConnectionManager connMgr = DBConnectionManager.getInstance();
	        try { TIMEOUT = Integer.parseInt(ConfigManager.getInstance().get(Constants.DB_WAIT_TIME)); } catch (Exception e) {}
	        Connection conn = connMgr.getConnection(connectionPool, TIMEOUT);
			
			try {
				String sql = "select COUNT(ifm_code) as counter from ifmcc where ifm_code = ?";
				
				PreparedStatement statement = conn.prepareStatement(sql);
				
				try {
					statement.setInt(1, ifm_mcc);
					
					ResultSet resultSet = statement.executeQuery();
					
					try {
						if(resultSet.next()){
							Interchange i = new Interchange();
							i.setIfmcode(Long.parseLong(resultSet.getString("counter")));
							
							return i;
						}
					} finally {
						resultSet.close();
					}
				} finally {
					statement.close();
				}
				
			} finally {
				connMgr.freeConnection(connectionPool, conn);
			}
			
			this.message = "Mcc code not found.";
			return null;
			
		} catch (Exception e){
			this.message = e.getMessage();
			return null;
		}
	}
	
	
}
